<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>健康服务系统</title>
    <meta name="description" content="健康服务系统">
    <meta name="keywords" content="健康服务系统">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .el-main {
            position: absolute;
            top: 70px;
            bottom: 0px;
            left: 200px;
            right: 10px;
            padding: 0;
        }
    </style>
</head>
<body class="hold-transition skin-purple sidebar-mini">
<div id="app">
    <el-container>
        <el-header class="main-header" style="height:70px;">
            <nav class="navbar navbar-static-top" :class=''>
                <!-- Logo -->
                <a href="/pages/main.html" style="display: inline-block; width: 600px">
                    <span style="display: inline-block; color: #fff;font-size: 25px;margin:15px">
                        健康服务系统
                    </span>
                </a>
                <div class="right-menu">
                    <!--<span class="help"><i class="fa fa-exclamation-circle" aria-hidden="true"></i>帮助</span>-->
                    <el-dropdown class="avatar-container right-menu-item" trigger="click">
                        <div class="avatar-wrapper">
                            <img src="../img/user2-160x160.jpg" class="user-avatar">
                            <!--展示用户名-->
                            {{username}}
                        </div>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided>
                                <span style="display:block;" @click="showUpdatePassword">修改密码</span>
                            </el-dropdown-item>
                            <el-dropdown-item divided>
                                <span style="display:block;"><a href="/logout.do">退出</a></span>
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </div>
            </nav>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu>
                    <el-submenu v-for="(menu,index) in menuList" :index="menu.path" :key="menu.id">
                        <template slot="title">
                            <i class="fa" :class="menu.icon"></i>
                            {{menu.name}}
                        </template>
                        <template v-for="child in menu.children">
                            <el-menu-item :index="child.path">
                                <a :href="child.linkUrl" target="right">{{child.name}}</a>
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-container>
                <iframe name="right" class="el-main" :src="mainSrc" width="100%" height="100%"
                        frameborder="0"></iframe>
            </el-container>
        </el-container>
    </el-container>

    <el-dialog title="修改密码" :visible.sync="updatePasswordFormVisible">
        <el-form :model="form">
            <el-form-item label="用户名" :label-width="form.username">
                <el-input v-model="form.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" :label-width="form.password">
                <el-input v-model="form.password" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updatePasswordFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updatePassword">确 定</el-button>
        </div>
    </el-dialog>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index_copy.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            username: null,//用户名
            menuList: [],
            mainSrc: "",
            form: {},
            //用于打开修改密码的框
            updatePasswordFormVisible: false,
        },
        //
        /*created() {
            this.getUsername()
        },*/
        mounted() {
            this.getUsername();
        },
        methods: {
            showUpdatePassword() {
                this.updatePasswordFormVisible = true;
                this.form.password = ''
            },
            async updatePassword() {
                //密码是否为空
                if (this.form.password == '' || this.form.password == undefined || this.form.password == null) {
                    this.$message({
                        type: 'warning',
                        message: '密码不能为空'
                    })
                    return;
                }
                //用户名是否为空
                if (this.form.username == '' || this.form.username == undefined || this.form.username == null) {
                    this.$message({
                        type: 'warning',
                        message: '用户名不能为空'
                    })
                    return;
                }
                //获取数据
                let result = await axios.post('/user/updatePassword.do', this.form);
                this.$message({
                    type: result.data.flag ? 'success' : "error",
                    message: result.data.message
                })
                if (result.data.flag) {
                    //跳转至登录页面
                    location.href = '/logout.do'
                }
            },
            async getUsername() {
                //发送请求获取当前登录用户的用户名
                let res = await axios.get('/user/getUsername.do');
                if (res.data.flag) {
                    this.username = res.data.data;
                    await this.getMenu();
                    let result = await axios.get('/user/getUserByUserName.do?userName=' + this.username);
                    let user = result.data.data;
                    this.form = user
                    localStorage.setItem('userInfo', JSON.stringify(user))
                }

            },

            async getMenu() {
                //获取菜单列表
                axios.get("/menu/getMenu.do?userName=" + this.username).then((result) => {
                    this.menuList = result.data.data
                    // console.log(this.menuList)
                    this.defaultMain()
                })
            },
            defaultMain() {
                for (let i = 0; i < this.menuList.length; i++) {
                    if (!this.menuList[i].linkUrl) {
                        //遍历子菜单
                        for (let j = 0; j < this.menuList[i].children.length; j++) {
                            if (this.menuList[i].children[j].linkUrl != undefined) {
                                this.mainSrc = this.menuList[i].children[j].linkUrl
                                return;
                            }
                        }
                    } else {
                        this.mainSrc = this.menuList[i].linkUrl
                        return;
                    }
                }
            }
        }
    });
    $(function () {
        let wd = 200;
        $(".el-main").css('width', $('body').width() - wd + 'px');
    });
</script>
</html>
